package com.wang.designpattern.mediator;

import java.util.HashMap;
import java.util.Map;

/**
 * 具体的中介者类
 */
public class ConcreteMediator extends Mediator {

    private Map<String, Colleague> colleagueMap;

    private Map<String, String> interMap;

    public ConcreteMediator() {
        colleagueMap = new HashMap<>();
        interMap = new HashMap<>();
    }

    @Override
    public void register(String colleagueName, Colleague colleague) {
        colleagueMap.put(colleagueName, colleague);
        if (colleague instanceof Alarm) {
            interMap.put("Alarm", colleagueName);
        } else if (colleague instanceof TV) {
            interMap.put("TV", colleagueName);
        } else if (colleague instanceof Curtain) {
            interMap.put("Curtain", colleagueName);
        } else if (colleague instanceof CoffeeMachine) {
            interMap.put("CoffeeMachine", colleagueName);
        }
    }

    @Override
    public void getMessage(int stateChange, String colleagueName) {
        // 表示从那个同事对象得到的消息
        if (colleagueMap.get(colleagueName) instanceof Alarm) {
            // 根据消息不同，做不同的动作
            if (0 == stateChange) {
                colleagueMap.get(interMap.get("CoffeeMachine")).receiveMessage(0);
                colleagueMap.get(interMap.get("TV")).receiveMessage(0);
            } else if (1 == stateChange) {
                colleagueMap.get(interMap.get("TV")).receiveMessage(1);
            }
        } else if (colleagueMap.get(colleagueName) instanceof CoffeeMachine) {
            if (0 == stateChange) {
                colleagueMap.get(interMap.get("Curtain")).receiveMessage(0);
            } else if (1 == stateChange) {
                colleagueMap.get(interMap.get("Curtain")).receiveMessage(1);
            }
        } else if (colleagueMap.get(colleagueName) instanceof TV) {
            if (0 == stateChange) {
                colleagueMap.get(interMap.get("TV")).receiveMessage(0);
            } else if (1 == stateChange) {
                colleagueMap.get(interMap.get("TV")).receiveMessage(1);
            }
        } else if (colleagueMap.get(colleagueName) instanceof Curtain) {
            if (0 == stateChange) {
                colleagueMap.get(interMap.get("Curtain")).receiveMessage(0);
            } else if (1 == stateChange) {
                colleagueMap.get(interMap.get("Curtain")).receiveMessage(1);
            }
        }
    }

    @Override
    public void sendMessage() {

    }
}
